#include "napi/native_api.h"
#include "common.h"
#include <asm-generic/stat.h>
#include <ace/xcomponent/native_interface_xcomponent.h>
#include "gles/glesmain.h"
glesmain glesmain_;
void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window) { glesmain_.OnSurfaceCreateCB(component,window); }
void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window)
{
    
}
void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)
{
    
}
void DispatchTouchEventCB(OH_NativeXComponent *component, void *window)
{
    MLOG("onTouch");
}

void DispatchMouseEventCB(OH_NativeXComponent* component, void* window)
{
    
}
void DispatchHoverEventCB(OH_NativeXComponent* component, bool isHover)
{
    
}

//回调对象传的指针，不能销毁
static OH_NativeXComponent_Callback renderCallback_;
static OH_NativeXComponent_MouseEvent_Callback mouseCallback_;
static void xcomponent_init(napi_env env, napi_value exports) {
    //初始化XComponent
    //OH_LOG_Print(LOG_APP, LOG_INFO, LOG_PRINT_DOMAIN, "===ATGame===Init", "初始化 XComponent");
    MLOG("xcomponent_init.初始化 XComponent");
    napi_value exportInstance = nullptr;
    if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
        MLOG("Export: napi_get_named_property fail");
        return;
    }
    MLOG("xcomponent_init.得到 XComponent Instance");

    OH_NativeXComponent *nativeXComponent = nullptr;
    if (napi_unwrap(env, exportInstance, reinterpret_cast<void **>(&nativeXComponent)) != napi_ok) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager", "Export: napi_unwrap fail");
        return;
    }
    MLOG("xcomponent_init.得到 XComponent 指针");

    char idStr[OH_XCOMPONENT_ID_LEN_MAX + 1] = {'\0'};
    uint64_t idSize = OH_XCOMPONENT_ID_LEN_MAX + 1;
    if (OH_NativeXComponent_GetXComponentId(nativeXComponent, idStr, &idSize) != OH_NATIVEXCOMPONENT_RESULT_SUCCESS) {
        OH_LOG_Print(LOG_APP, LOG_ERROR, LOG_PRINT_DOMAIN, "PluginManager",
                     "Export: OH_NativeXComponent_GetXComponentId fail");
        return;
    }


    MLOGFormat("component_init.得到 XComponent id =%{public}s", idStr);
    // 莫名奇妙的crash，问题出在哪里了？？
    // Log("xcomponent_init.得到 XComponent id %s", idStr);
    // 设置回调
    MLOGFormat("xcomponent_init thread=%{public}16x", getThreadID());


    renderCallback_.OnSurfaceCreated = OnSurfaceCreatedCB;
    renderCallback_.OnSurfaceChanged = OnSurfaceChangedCB;
    renderCallback_.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
    renderCallback_.DispatchTouchEvent = DispatchTouchEventCB;
    OH_NativeXComponent_RegisterCallback(nativeXComponent, &renderCallback_);

    mouseCallback_.DispatchMouseEvent = DispatchMouseEventCB;
    mouseCallback_.DispatchHoverEvent = DispatchHoverEventCB;
    OH_NativeXComponent_RegisterMouseEventCallback(nativeXComponent, &mouseCallback_);

}


EXTERN_C_START
static napi_value Init(napi_env env, napi_value exports) {

    xcomponent_init(env,exports);
    // napi_property_descriptor desc[] = {{"add", nullptr, Add, nullptr, nullptr, nullptr, napi_default, nullptr}};
    // napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}
EXTERN_C_END

static napi_module mainModule = {
    .nm_version = 1,
    .nm_flags = 0,
    .nm_filename = nullptr,
    .nm_register_func = Init,
    .nm_modname = "arktsgame",
    .nm_priv = ((void *)0),
    .reserved = {0},
};

extern "C" __attribute__((constructor)) void RegisterEntryModule(void) { napi_module_register(&mainModule); }
